import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class PictureTakePhoto extends StatefulWidget {
  @override
  _PictureTakePhotoState createState() => _PictureTakePhotoState();
}

class _PictureTakePhotoState extends State<PictureTakePhoto>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Future<File> _imageFile;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text('相册照相'),
      ),
      body: Container(
          //垂直布局时：y轴为主轴，x轴为副轴!
          //水平不就是：x轴为主轴，y轴为副轴!
          padding: EdgeInsets.only(left: 10, right: 10),
          // width: MediaQuery.of(context).size.width,
          color: Colors.green,
          child: rowWidget()),
    );
  }

  Widget columWidget() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center, //水平
      mainAxisAlignment: MainAxisAlignment.center, //竖直
      children: <Widget>[
        Container(
          color: Colors.red,
          child: Text('很长很长很长很长很长很长'),
        ),
        _previewImage(),
        FlatButton(
          onPressed: () {
            print('相册');
            _selectedImage();
          },
          child: Text('相册'),
        ),
        FlatButton(
          onPressed: () {
            print('相机');
            _takePhoto();
          },
          child: Text('相机'),
        ),
      ],
    );
  }

  Widget rowWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center, //竖直
      crossAxisAlignment: CrossAxisAlignment.center, //水平
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width / 5,
          color: Colors.red,
          child: Text('jldfjajdfk'),
        ),
        _previewImage(),
        FlatButton(
          onPressed: () {
            print('相册');
            _selectedImage();
          },
          child: Text('相册'),
        ),
        FlatButton(
          onPressed: () {
            print('相机');
            _takePhoto();
          },
          child: Text('相机'),
        ),
      ],
    );
  }

  void _selectedImage() {
    setState(() {
      _imageFile = selectImage(ImageSource.gallery);
    });
  }

  void _takePhoto() {
    setState(() {
      _imageFile = selectImage(ImageSource.camera);
    });
  }

  static Future<File> selectImage(ImageSource source) async {
    var file = await ImagePicker().getImage(source: source, imageQuality: 30);
    return File(file.path);
  }

  Widget _previewImage() {
    return FutureBuilder<File>(
        future: _imageFile,
        builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
          if (snapshot.connectionState == ConnectionState.done &&
              snapshot.data != null) {
            return new ClipOval(
              child: SizedBox(
                  width: 70.0,
                  height: 70.0,
                  child: Image.file(snapshot.data, fit: BoxFit.cover)),
            );
          } else {
            return Text('没图');
          }
        });
  }
}
